/**
 * 验证码
 */

export const showHideModal= (self,tiyData,isShow) => {
  tiyData.tParam.codeParam.sendCodeMsg = '发送验证码';
  tiyData.tParam.codeParam.validateCode = '';
  tiyData.status.modalCode.isShow = isShow;
};

export const validateCode= (self,tiyData) => {
  if("" == tiyData.tParam.codeParam.validateCode){
    self.$Message.error("验证码不能为空");
    return;
  }
  self.$Spin.show();
  tiyData.tParam.codeParam.isLoading = true;
    self.$apiOther.validateCode({userId:self.userId,code:tiyData.tParam.codeParam.validateCode})
    .then(function (data) {
      self.$Spin.hide();
      if(data.success){
        self.$emit('validateCodeEnd',data.success)
        tiyData.tParam.codeParam.sendCodeMsg = '发送验证码';
        tiyData.tParam.codeParam.validateCode = '';
        tiyData.status.modalCode.isShow = false;
        self.codeParam.sendDisabled = false;
        tiyData.tParam.codeParam.totalTime = 60;
      }else {
        self.$Message.error("验证失败");
      }
    });

};
export const sendValidateCode= (self,tiyData) => {
  tiyData.tParam.codeParam.isSending = true;
  tiyData.tParam.codeParam.totalTime = 60;
  self.$apiOther.sendValidateCode({phoneNum:self.phoneNum,userId:self.userId}).then(function (response) {
    tiyData.tParam.codeParam.isSending = false;
    if (response.success) {
      self.codeParam.sendDisabled = true;
      let clock = window.setInterval(() => {
        tiyData.tParam.codeParam.totalTime--;
        self.codeParam.sendCodeMsg = tiyData.tParam.codeParam.totalTime + 's后重新发送';
        if (tiyData.tParam.codeParam.totalTime <= 0) {     //当倒计时小于0时清除定时器
          window.clearInterval(clock);
          self.codeParam.sendDisabled = false;
          self.codeParam.sendCodeMsg = '重新发送验证码';
          tiyData.tParam.codeParam.totalTime = 60;
        }
      },1000);
    }else {
      self.$Message.error(response.errorMessage)
      self.codeParam.sendCodeMsg = "重试";
      self.codeParam.sendDisabled = false
    }
  });

};
